package pw.yumc.Residence.listeners.bugfix;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.FlagPermissions;

import pw.yumc.Residence.ResidenceMain;

public class ResidenceBugFix implements Listener {
    ResidenceMain plugin;

    public ResidenceBugFix(final ResidenceMain plugin) {
        this.plugin = plugin;
        plugin.getLogger().info("防刷甘蔗补丁已加载...");
        plugin.getLogger().info("防刷单元补丁已加载...");
        plugin.getLogger().info("防刷生物补丁已加载...");
    }

    public Block getPlaceBlock(final PlayerInteractEvent e) {
        final Player p = e.getPlayer();
        final ItemStack it = p.getItemInHand();
        if (it == null || it.getType() == Material.AIR) {
            return null;
        }
        final Block b = e.getClickedBlock();
        final BlockFace bf = e.getBlockFace();
        if (bf == null) {
            return null;
        }
        return b.getRelative(bf, 1);
    }

    public Block getPlaceBlockDown(final PlayerInteractEvent e) {
        final Block b = this.getPlaceBlock(e);
        return b.getRelative(BlockFace.DOWN);
    }

    public boolean hasPerm(final ClaimedResidence res, final Player p) {
        final FlagPermissions perms = res.getPermissions();
        final String world = p.getWorld().getName();
        return perms.playerHas(p.getName(), world, "place", perms.playerHas(p.getName(), world, "build", false));
    }

    @EventHandler
    public void onBlockPlace(final PlayerInteractEvent e) {
        if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.PHYSICAL) {
            return;
        }
        try {
            final Block b = this.getPlaceBlockDown(e);
            if (b == null) {
                return;
            }
            final Player p = e.getPlayer();
            final ItemStack it = p.getItemInHand();
            final ClaimedResidence res = plugin.getResidenceManager().getByLoc(b.getLocation());
            if (res == null) {
                return;
            }
            if ((it.getType() == Material.PUMPKIN && (b.getType() == Material.SNOW_BLOCK || b.getType() == Material.IRON_BLOCK))
                    || (it.getType() == Material.SKULL_ITEM && b.getType() == Material.SOUL_SAND)) {
                if (!hasPerm(res, p)) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission") + ChatColor.DARK_RED + " 当前区域不允许放置!");
                }
            }
        } catch (final Exception e2) {
        }
    }

    @EventHandler
    public void onPlace(final PlayerInteractEvent e) {
        if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.PHYSICAL) {
            return;
        }
        try {
            final Player p = e.getPlayer();
            final ItemStack it = p.getItemInHand();
            if (it == null || it.getType() == Material.AIR) {
                return;
            }
            final Block b = e.getClickedBlock();
            final BlockFace bf = e.getBlockFace();
            if (bf == null || bf != BlockFace.UP) {
                return;
            }
            final Block b1 = b.getRelative(bf, 1);
            final Block b2 = b.getRelative(bf, 2);
            final ClaimedResidence res = plugin.getResidenceManager().getByLoc(b1.getLocation());
            if (res == null) {
                return;
            }
            if (b1.getType() != Material.AIR && b1.getType() != Material.TORCH && b2.getType() == Material.AIR && checkAround(b2)) {
                if (!hasPerm(res, p)) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission") + ChatColor.DARK_RED + " 当前区域不允许此操作!");
                }
            }
        } catch (final Exception e2) {
        }
    }

    private boolean checkAround(final Block b) {
        final Block be = b.getRelative(BlockFace.EAST);
        final Block bw = b.getRelative(BlockFace.WEST);
        final Block bn = b.getRelative(BlockFace.NORTH);
        final Block bs = b.getRelative(BlockFace.SOUTH);
        return (be.getType() != Material.AIR || bw.getType() != Material.AIR || bn.getType() != Material.AIR || bs.getType() != Material.AIR);
    }
}
